<template>
  <div>
    <div style="position: relative">
      <el-upload
        :action="uploadUrl"
        :show-file-list="false"
        :before-upload="beforeUploadVideo"
        :on-progress="uploadVideoProcess"
        :http-request="uploadFileVieo">
        <video v-if="showVideoPath !='' && videoFlag == false" :src="showVideoPath" class="avatar" controls="controls">您的浏览器不支持视频播放</video>
        <el-button v-else-if="showVideoPath =='' && videoFlag == false" size="small" type="primary">上传视频</el-button>
        <!--<i v-else-if="showVideoPath =='' && videoFlag == false" class="el-icon-plus avatar-uploader-icon"></i>-->
        <el-progress v-if="videoFlag == true" :percentage="videoUploadPercent" type="circle" style="margin-top:30px;"/>
      </el-upload>
      <i v-if="showVideoPath !='' && videoFlag == false" class="el-icon-delete" @click="delmp4"/>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  /* eslint-disable vue/require-prop-types */
  props: ['type', 'editorData'],
  data() {
    return {
      uploadUrl: '',
      fileList: {},
      // 上传视频
      videoFlag: false,
      videoUploadPercent: 0,
      showVideoPath: '' // 视频地址
    }
  },
  beforeMount() {
    if (this.type !== 1) {
      this.showVideoPath = this.editorData.otherUrl
      this.fileList.otherUrl = this.editorData.otherUrl
      this.fileList.fristFrameUrl = this.editorData.fristFrameUrl
    }
  },
  created() {
    this.initUploadFileurl()
  },
  methods: {
    initUploadFileurl() {
      this.uploadUrl = process.env.BASE_API + 'file/uploadVideoAndfirstFrame'
    },
    beforeUploadVideo(file) {
      const isLt20M = file.size / 1024 / 1024 < 20
      if (['video/mp4'].indexOf(file.type) === -1) { // 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb'
        this.$message.error('请上传正确的视频格式')
        return false
      }
      if (!isLt20M) {
        this.$message.error('上传视频大小不能超过20MB哦!')
        return false
      }
    },
    uploadVideoProcess(event, file, fileList) {
      this.videoFlag = true
      this.videoUploadPercent = file.percentage.toFixed(0)
    },
    uploadFileVieo(content) {
      console.log('内容：')
      console.log(content)
      const fd = new FormData()
      fd.append('files', content.file)
      const loading = this.$loading({
        lock: true,
        text: '上传中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      axios({
        url: content.action,
        method: 'POST',
        // filePath: fd,
        headers: {
          'Content-Type': 'multipart/form-data'
        }, // this is important !
        contentType: false,
        processData: false,
        // name: 'files',
        data: fd,
        params: fd

      }).then(res => {
        loading.close()
        if (res.data.code === 1) {
          this.$message({
            message: '上传成功',
            type: 'success'
          })
          this.videoFlag = false
          this.videoUploadPercent = 0
          this.showVideoPath = this.HOSTURL + '' + res.data.data[0].videoUrl

          this.fileList.otherUrl = this.showVideoPath
          this.fileList.fristFrameUrl = this.HOSTURL + '' + res.data.data[0].firstFrameUrl
        } else {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          })
        }
      }).catch(err => {
        console.error(err)
        this.$message.error('上传视频失败')
        loading.close()
      })
    },
    delmp4() {
      this.showVideoPath = ''
      this.fileList = {}
      this.$emit('emitSuccess', this.fileList)
    },
    incrementTotal() {
      this.$emit('emitSuccess', this.fileList)
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
  .avatar{
    width: 300px;
    height: 200px;
  }
  .el-icon-delete{
    position: absolute;
    left: 320px;
    bottom: 10px;
    cursor: pointer;
  }
</style>
